package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//自定义列表
public class MyList extends AppCompatActivity implements Runnable{
    String[] data = {"wait..."};
    private static final String TAG ="mylist" ;
    Handler handler;
    //String[] list_data = {"one","two","three","four"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_list); //父类为ListActivity不能用
//        for(int i=1;i<100;i++){
//            list1.add("item"+i);
//       }
        ListView listview = (ListView)findViewById(R.id.mylist);  //获取ListView控件
        List<String> list1 = new ArrayList<String>();
        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data);
        listview.setAdapter(adapter);  //显示wait...
        //开启子线程
        Thread t = new Thread(this);
        t.start();

        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if(msg.what==5){
                    List<String> list2 = (List<String>) msg.obj;
                    //构造适配器对象
                    ListAdapter adapter = new ArrayAdapter<String>(MyList.this, android.R.layout.simple_list_item_1,list2);
                    listview.setAdapter(adapter);  //对控件使用该方法（在默认类下）
                   // setListAdapter(adapter);    必须在父类ListActivity中才能用
                }
                super.handleMessage(msg);
            }
        };
    }

    @Override
    public void run() {
        //获取网络数据
        Log.i(TAG, "run: run()......");
        for(int i=1;i<=3;i++) {
            Log.i(TAG, "run: i=" + i);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        List<String> retList = new ArrayList<String>();
        try {
            Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();  //document必须选择jsoup版

            Elements tables = doc.getElementsByTag("table");
            Element table = tables.get(1);  //注意是从零开始的
//            Log.i(TAG, "run: table=" + table);
            Elements tds = table.getElementsByTag("td");  //按标志td查找

            for(int i=0;i<tds.size();i+=8){
                    //提取数据
                    Element td1 = tds.get(i); //名称
                    Element td2 = tds.get(i+5);  //汇率值

                    Log.i(TAG, "run" + td1.text() + "-->" + td2.text());
                    String str = td1.text();  //!!将td1也要转成字符否则无法识别
                    String r = td2.text(); //将td2转成String类型

                    retList.add(str + "==>" + r);

            }

        }  catch (IOException e) {
            e.printStackTrace();
        }
        Message msg = handler.obtainMessage(5);
        msg.obj = retList;  //放入List带回
        handler.sendMessage(msg);
    }
}